﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using W.Warehouse.Api;
using W.Warehouse.Api.Application.CommandHandlder.StorageCommandHandlder;
using W.Warehouse.Domain.Reportlosses;
using W.Warehouse.Read.Api.Application.Command.ReportlossesCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Product.API.Appliaction.CommandHandler.ReportlossesCommandHandler
{
    public class ReportlossesCreateCommandHandler : IRequestHandler<ReportlossesCreateCommand, ApiResult<int>>
    {
        private readonly ILogger<ReportlossesCreateCommandHandler> _logger;
        private readonly IReportlossesRepository _reportlossesRepository;
        private readonly IReportProductRepository _reportProductRepository;
        public ReportlossesCreateCommandHandler(IReportlossesRepository reportlossesRepository,
           IReportProductRepository reportProductRepository, ILogger<ReportlossesCreateCommandHandler> logger)
        {
            _reportlossesRepository = reportlossesRepository;
            _reportProductRepository = reportProductRepository;
            _logger=logger;
        }
        /// <summary>
        /// 添加报损管理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(ReportlossesCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            _logger.LogInformation("添加入库成功");
            using (var tran = _reportlossesRepository.DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    var now=DateTime.Now;

                    ReportlossesInfo info = new ReportlossesInfo()
                    {
                        ReportlossesNo = request.ReportlossesNo,
                        ReportlossesType = request.ReportlossesType,
                        AssociateNumber = request.AssociateNumber,
                        SingleMaker = request.SingleMaker,
                        MakeTime = request.MakeTime,
                        MoveRemark = request.MoveRemark,
                        DocumentState = Common.Enum.AuditStatus.等待审核,
                        ExamineReason = request.ExamineReason,
                        ExaminePeople = "yu",
                        ExamineTime =DateTime.Now,
                        Wlocator = "仓库3：默认正式库位",
                        WRepolocator = "仓库2：默认正式库位",
                        CreateTime = now,
                        officeId = request.officeId,
                        userid = request.userid,
                        CreateName = "admin",
                        IsDelete = false,
                    };
                     await _reportlossesRepository.AddAsync(info);
                    foreach (var item in request.ReportProductInfo)
                    {
                        ReportProduct report = new ReportProduct()
                        {
                            ReportlossesId=info.ReportlossesId,
                            ProductNo = item.ProductNo,
                            ProductName = item.ProductName,
                            ProductSpec = item.ProductSpec,
                            Batch = "20241119",
                            Price = item.Price,
                            ReportNum = item.ReportNum,
                            BinLocationName ="1号仓库",
                            CreateTime = now,
                            officeId = request.officeId,
                            userid = request.userid,
                            CreateName = "admin",
                            IsDelete = false,
                        };
                        await _reportProductRepository.AddAsync(report);
                    }
                   var list=await _reportlossesRepository.DB.SaveChangesAsync();
                    tran.Commit();
                    result.Code = list;
                }
                catch (Exception el)
                {
                    result.Code = -1;
                    result.Message = el.Message;
                    _logger.LogError(el, el.Message);
                    tran.Rollback();
                    throw;
                }
            
            }

            return result;



              
        }
    }
}
